//
//  HMComponseView.swift
//  swift微博项目
//
//  Created by huodian on 16/10/6.
//  Copyright © 2016年 huodian. All rights reserved.
//

import UIKit

import pop

class HMComponseView: UIView {

    lazy var btnArray = [HMComponseBtn]()
    
    var targetController : UIViewController?
    
    
    override init(frame: CGRect) {
        super.init(frame: UIScreen.main.bounds)
        
        self.backgroundColor = random()
        
        setMyUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

    func show(target:UIViewController){
        
        target.view.addSubview(self)
        
        self.targetController = target
        
        
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        
     
        
        
        for btn in self.btnArray.reversed().enumerated() {
            
            btnAnimation(btn: btn.element, index: Double(btn.offset), toput: false)
   /*
            let pop  =  POPSpringAnimation.init(propertyNamed: kPOPViewCenter)
            
            pop?.springSpeed = 10
            
            pop?.springBounciness = 10
            
            pop?.toValue = NSValue.init(cgPoint: CGPoint(x: btn.center.x, y: btn.center.y + 350))
            
            pop?.beginTime = CACurrentMediaTime() + index * 0.25
            
            
            btn.pop_add(pop, forKey: nil)
    */
        
            
            DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.5, execute: { 
                
                self.removeFromSuperview()
                
            })
            
        }
        
        
        
    }
    
    override func willMove(toSuperview newSuperview: UIView?) {
        
   
        
        
        for btn in btnArray.enumerated() {
            
            btnAnimation(btn: btn.element, index: Double(btn.offset), toput: true)
            
   /*
            let shack = POPSpringAnimation(propertyNamed:kPOPViewCenter)
            
            shack?.springBounciness = 10
            
            shack?.springSpeed = 10
            
            shack?.beginTime = CACurrentMediaTime() + index * 0.25
            
            shack?.toValue = NSValue.init(cgPoint: CGPoint(x: btn.center.x, y: (btn.center.y-350)))
            
            btn.pop_add(shack, forKey: nil)
  */
        
            
        }
        
       
        
    }
    
    
    func btnAnimation(btn:HMComponseBtn,index:Double,toput:Bool){
        
        let shack = POPSpringAnimation(propertyNamed:kPOPViewCenter)
        
        shack?.springBounciness = 10
        
        shack?.springSpeed = 10
        
        shack?.beginTime = CACurrentMediaTime() + index * 0.25
        
        shack?.toValue = NSValue.init(cgPoint: CGPoint(x: btn.center.x, y: btn.center.y +  (toput == true ? -350 : 350 )))
        
        btn.pop_add(shack, forKey: nil)
        
        
    }
    
    func setMyUI(){
        
       let backImageView = UIImageView(image:UIImage.snpClipImage().applyLightEffect())
        
        
        self.addSubview(backImageView)
        
        
       
    let titleImage = UIImageView(image: UIImage(named: "compose_slogan"))
        
        self.addSubview(titleImage)
        
        titleImage.snp.makeConstraints { (make) in
            
            make.centerX.equalTo(self.snp.centerX)
            
            make.centerY.equalTo(self.snp.centerY).offset(-100)
            
        }
        
       addTheBottomBtn()
        
    }
    
    
    
    
    func addTheBottomBtn(){
    
        let margin = CGFloat((screenWidth - 3 * btnW ) / (3 + 1))
        
        for i in 0..<6 {
            
            let col = CGFloat( i % 3 )
            
            let row  = CGFloat( i / 3)
            
            let btnX = margin + col * (margin + btnW)
            
            let btnY = margin + row * (margin + btnH) + screenHeight
            
            let btn  = HMComponseBtn()
            
            btn.frame = CGRect(x: btnX, y: btnY, width: btnW, height: btnH)
            
            btn.setImage(UIImage(named: "tabbar_compose_music"), for: UIControlState.normal)
            
            btn.setTitle("音乐", for: UIControlState.normal)
            
            
            btn.addTarget(self, action: #selector(clickTheFunctionBtn(btn:)), for: UIControlEvents.touchUpInside)
            
            
            btnArray.append(btn)
            
            self.addSubview(btn)
            
            
            
            
            
            
        }
     
    }
    func clickTheFunctionBtn(btn:HMComponseBtn){
        
        for btnC in self.btnArray {
            
            btnC.alpha = 0.1
            
            UIView.animate(withDuration: 1, animations: {
                
                
                
                if btnC == btn {
                    print("5555")
                    btn.transform = CGAffineTransform.init(scaleX: 2, y: 2)
                }else
                {
                        print("55551111")
                    btn.transform = CGAffineTransform.init(scaleX: 0.1, y: 0.1)
                }

                
                }, completion: { (_) in
                    
                    let bundleName = Bundle.main.infoDictionary!["CFBundleName"] as! String
                    
                    let realName = bundleName + "." + "HMComponseVIewController"
                    
                    let type = NSClassFromString(realName) as! UIViewController.Type
                    
                    let vc = type.init()
                
                    let nav = UINavigationController(rootViewController: vc)
                    
                    
                    self.targetController?.present(nav, animated: true, completion: { 
                        
                        self.removeFromSuperview()
                        
                    })
                    
                    
            })
            
            
        }
        
        
        
    }
}
